home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Services / Weather.php
PHP Script  |  2004-10-01  |  9KB  |  237 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexander Wirtz <alex@pc4p.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Weather.php,v 1.31 2004/05/04 13:47:41 eru Exp $
  20.  
  21. /**
  22. * @package      Services_Weather
  23. * @filesource
  24. */
  25.  
  26. /**
  27. */
  28. // {{{ constants
  29. // {{{ cache times
  30. define("SERVICES_WEATHER_EXPIRES_UNITS",      900);
  31. define("SERVICES_WEATHER_EXPIRES_LOCATION",   900);
  32. define("SERVICES_WEATHER_EXPIRES_WEATHER",   1800);
  33. define("SERVICES_WEATHER_EXPIRES_FORECAST",  7200);
  34. define("SERVICES_WEATHER_EXPIRES_LINKS",    43200);
  35. // }}}
  36.  
  37. // {{{ error codes
  38. define("SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND",   10);
  39. define("SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION",    11);
  40. define("SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA",   12);
  41. define("SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED",   13);
  42. define("SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED",    14);
  43. // }}}
  44.  
  45. // {{{ error codes defined by weather.com
  46. define("SERVICES_WEATHER_ERROR_UNKNOWN_ERROR",            0);
  47. define("SERVICES_WEATHER_ERROR_NO_LOCATION",              1);
  48. define("SERVICES_WEATHER_ERROR_INVALID_LOCATION",         2);
  49. define("SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID",     100);
  50. define("SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE",   101);
  51. define("SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY",    102);
  52. // }}}
  53. // }}}
  54.  
  55. // {{{ class Services_Weather
  56. /**
  57. * PEAR::Services_Weather
  58. *
  59. * This class acts as an interface to various online weather-services.
  60. *
  61. * Services_Weather searches for given locations and retrieves current
  62. * weather data and, dependant on the used service, also forecasts. Up to
  63. * now, SOAP services from CapeScience and EJSE, XML from weather.com and
  64. * METAR/TAF from noaa.gov are supported, further services will get
  65. * included, if they become available and are properly documented.
  66. *
  67. * @author       Alexander Wirtz <alex@pc4p.net>
  68. * @package      Services_Weather
  69. * @license      http://www.php.net/license/2_02.txt
  70. * @version      1.3
  71. */
  72. class Services_Weather {
  73.  
  74.     // {{{ &service()
  75.     /**
  76.     * Factory for creating the services-objects
  77.     *
  78.     * Usable keys for the options array are:
  79.     * o debug               enables debugging output
  80.     * --- Common Options
  81.     * o cacheType           defines what type of cache to use
  82.     * o cacheOptions        passes cache options
  83.     * o unitsFormat         use (US)-standard, metric or custom units
  84.     * o customUnitsFormat   defines the customized units format
  85.     * o httpTimeout            sets timeout for HTTP requests
  86.     * o dateFormat          string to use for date output
  87.     * o timeFormat          string to use for time output
  88.     * --- EJSE Options
  89.     * o none
  90.     * --- GlobalWeather Options
  91.     * o none
  92.     * --- METAR/TAF Options
  93.     * o dsn                 String for defining the DB connection
  94.     * o dbOptions           passes DB options
  95.     * o sourceMetar         http, ftp or file - type of data-source for METAR
  96.     * o sourcePathMetar     where to look for the source, URI or filepath,
  97.     *                       of METAR information
  98.     * o sourceTaf           http, ftp or file - type of data-source for TAF
  99.     * o sourcePathTaf       where to look for the source, URI or filepath,
  100.     *                       of TAF information
  101.     * --- weather.com Options
  102.     * o partnerID           You'll receive these keys after registering
  103.     * o licenseKey          with the weather.com XML-service
  104.     *
  105.     * @param    string                      $service
  106.     * @param    array                       $options
  107.     * @return   PEAR_Error|object
  108.     * @throws   PEAR_Error
  109.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND
  110.     * @access   public
  111.     */
  112.     function &service($service, $options = null)
  113.     {
  114.         $service = ucfirst(strtolower($service));
  115.         $classname = "Services_Weather_".$service;
  116.  
  117.         // Check for debugging-mode and set stuff accordingly
  118.         if (is_array($options) && isset($options["debug"]) && $options["debug"] >= 2) {
  119.             if (!defined("SERVICES_WEATHER_DEBUG")) {
  120.                 define("SERVICES_WEATHER_DEBUG", true);
  121.             }
  122.             include_once("Services/Weather/".$service.".php");
  123.         } else {
  124.             if (!defined("SERVICES_WEATHER_DEBUG")) {
  125.                 define("SERVICES_WEATHER_DEBUG", false);
  126.             }
  127.             @include_once("Services/Weather/".$service.".php");
  128.         }
  129.  
  130.         // No such service... bail out
  131.         if (!class_exists($classname)) {
  132.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND, __FILE__, __LINE__);
  133.         }
  134.  
  135.         // Create service and return
  136.         $error = null;
  137.         @$obj = &new $classname($options, $error);
  138.  
  139.         if (Services_Weather::isError($error)) {
  140.             return $error;
  141.         } else {
  142.             return $obj;
  143.         }
  144.     }
  145.     // }}}
  146.  
  147.     // {{{ apiVersion()
  148.     /**
  149.     * For your convenience, when I come up with changes in the API...
  150.     *
  151.     * @return   string
  152.     * @access   public
  153.     */
  154.    function apiVersion()
  155.     {
  156.         return "1.3";
  157.     }
  158.     // }}}
  159.  
  160.     // {{{ _errorMessage()
  161.     /**
  162.     * Returns the message for a certain error code
  163.     *
  164.     * @param    PEAR_Error|int              $value
  165.     * @return   string
  166.     * @access   private
  167.     */
  168.     function _errorMessage($value)
  169.     {
  170.         static $errorMessages;
  171.         if (!isset($errorMessages)) {
  172.             $errorMessages = array(
  173.                 SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND         => "Requested service could not be found.",
  174.                 SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION          => "Unknown location provided.",
  175.                 SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA         => "Server data wrong or not available.",
  176.                 SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED         => "Cache init was not completed.",
  177.                 SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED          => "MetarDB is not connected.",
  178.                 SERVICES_WEATHER_ERROR_UNKNOWN_ERROR             => "An unknown error has occured.",
  179.                 SERVICES_WEATHER_ERROR_NO_LOCATION               => "No location provided.",
  180.                 SERVICES_WEATHER_ERROR_INVALID_LOCATION          => "Invalid location provided.",
  181.                 SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID        => "Invalid partner id.",
  182.                 SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE      => "Invalid product code.",
  183.                 SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY       => "Invalid license key."
  184.             );
  185.         }
  186.  
  187.         if (Services_Weather::isError($value)) {
  188.             $value = $value->getCode();
  189.         }
  190.  
  191.         return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[SERVICES_WEATHER_ERROR_UNKNOWN_ERROR];
  192.     }
  193.     // }}}
  194.  
  195.     // {{{ isError()
  196.     /**
  197.     * Checks for an error object, same as in PEAR
  198.     *
  199.     * @param    PEAR_Error|mixed            $value
  200.     * @return   bool
  201.     * @access   public
  202.     */
  203.     function isError($value)
  204.     {
  205.         return (is_object($value) && (strtolower(get_class($value)) == "pear_error" || is_subclass_of($value, "pear_error")));
  206.     }
  207.     // }}}
  208.  
  209.     // {{{ &raiseError()
  210.     /**
  211.     * Creates error, same as in PEAR with a customized flavor
  212.     *
  213.     * @param    int                         $code
  214.     * @param    string                      $file
  215.     * @param    int                         $line
  216.     * @return   PEAR_Error
  217.     * @access   private
  218.     */
  219.     function &raiseError($code = SERVICES_WEATHER_ERROR_UNKNOWN_ERROR, $file = "", $line = 0)
  220.     {
  221.         // This should improve the performance of the script, as PEAR is only included, when
  222.         // really needed.
  223.         include_once "PEAR.php";
  224.  
  225.         $message = "Services_Weather";
  226.         if ($file != "" && $line > 0) {
  227.             $message .= " (".basename($file).":".$line.")";
  228.         }
  229.         $message .= ": ".Services_Weather::_errorMessage($code);
  230.  
  231.         return PEAR::raiseError($message, $code, PEAR_ERROR_RETURN, E_USER_NOTICE, "Services_Weather_Error", null, false);
  232.     }
  233.     // }}}
  234. }
  235. // }}}
  236. ?>
  237.